package struts.action;

import global.GlobalConstants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.springframework.beans.factory.annotation.Autowired;

import struts.form.RegeditForm;
import bean.UserObject;
import biz.RegeditBiz;
import dto.UserDto;

public class UserListAction extends BaseDispatchAction {
	@Autowired
	RegeditBiz regeditBiz;

	@Autowired
	UserObject userObject;

	/**
	 * 初期处理
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		saveToken(request);
		form.reset(mapping, request);
		// 进入注册页面
		return mapping.findForward("self");
	}

	/**
	 * 注册用户
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		validateToken(request);

		RegeditForm regeditForm = (RegeditForm) form;
		String password = regeditForm.getPassword();
		String confirmPassword = regeditForm.getConfirmPassword();
		// 密码与确认密码不一致
		if (StringUtils.isBlank(password) || !password.equals(confirmPassword)) {
			ActionMessages errors = new ActionMessages();
			errors.add("confirmPassword", new ActionMessage("Z0001X0001"));
			saveErrors(request, errors);
			return mapping.getInputForward();
		}
		
		// 得到生成时保存在session里的validateCode
		String validateCodeGenerated = (String)request.getSession().getAttribute(GlobalConstants.VALIDATE_CODE_KEY);
		// 得到用户输入的validateCode
		String validateCodeTyped = regeditForm.getValidateCode();
		
		// 验证码输入不一致
		if(validateCodeGenerated != null && !validateCodeGenerated.equalsIgnoreCase(validateCodeTyped)) {
			ActionMessages errors = new ActionMessages();
			errors.add("validateCode", new ActionMessage("Z0001X0001"));
			saveErrors(request, errors);
			return mapping.getInputForward();
		}

		UserDto userDto = new UserDto();

		try {
			PropertyUtils.copyProperties(userDto, regeditForm);
		} catch (Exception e) {
			e.printStackTrace();
		}

		regeditBiz.insertUser(userDto);

		// 回到主画面
		return mapping.findForward("sucess");
	}

	/**
	 * 删除用户
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward deleteUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		validateToken(request);

		RegeditForm regeditForm = (RegeditForm) form;

		UserDto userDto = new UserDto();

		try {
			PropertyUtils.copyProperties(userDto, regeditForm);
		} catch (Exception e) {
			e.printStackTrace();
		}

		regeditBiz.deleteUser(userDto);

		// 回到主画面
		return mapping.findForward("sucess");

	}
}
